package com.lujianfei.imagesearch.presenter;

import java.awt.image.BufferedImage;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

import com.lujianfei.imagesearch.Constants;
import com.lujianfei.imagesearch.contract.CanvasContract;
import com.lujianfei.imagesearch.contract.MainContract.View;
import com.lujianfei.imagesearch.model.MainModel;

public class CanvasPresenter extends CanvasContract.Presenter {

	MainModel model = new MainModel();
	String absolutePath;

	public CanvasPresenter(CanvasContract.View mView) {
		super(mView);
	}

	@Override
	public void addNoise(int type) {
		// TODO Auto-generated method stub
		BufferedImage image = mView.getImage();
		model.addNoise(image, type);
		setTitleByType(type);
		mView.updateImage();
	}

	@Override
	public void removeNoise(int type) {
		// TODO Auto-generated method stub
		BufferedImage image = mView.getImage();
		model.filter(image, type);
		setTitleByType(type);
		mView.updateImage();
	}

	@Override
	public void addGray(int type) {
		// TODO Auto-generated method stub
		BufferedImage image = mView.getImage();
		if (null == image) {
			return;
		}
		model.addGray(image, type);
		setTitleByType(type);
		mView.updateImage();
	}

	private void setTitleByType(int type) {
		switch (type) {
		case Constants.GRAY_MODE.MAX:
			mView.setTitle("灰度(最大值法)");
			break;
		case Constants.GRAY_MODE.MIN:
			mView.setTitle("灰度(最小值法)");
			break;
		case Constants.GRAY_MODE.AVE:
			mView.setTitle("灰度(均值法)");
			break;
		case Constants.GRAY_MODE.WEIGHT:
			mView.setTitle("灰度(加权法)");
			break;
		case Constants.NOISE_MODE.PEPPER_SALT_NOISE:
			mView.setTitle("添加噪点(椒盐噪声)");
			break;
		case Constants.NOISE_MODE.PEPPER_NOISE:
			mView.setTitle("添加噪点(椒噪声)");
			break;
		case Constants.NOISE_MODE.SALT_NOISE:
			mView.setTitle("添加噪点(盐噪声)");
			break;
		case Constants.REMOVE_NOISE_MODE.MAXMIN_FILTER:
			mView.setTitle("去噪(最大最小值滤波)");
			break;
		case Constants.REMOVE_NOISE_MODE.MEAN_FILTER:
			mView.setTitle("去噪(均值滤波)");
			break;
		case Constants.REMOVE_NOISE_MODE.MEDIUM_FILTER:
			mView.setTitle("去噪(中值滤波)");
			break;

		default:
			break;
		}
	}

	@Override
	public void restore() {
		// TODO Auto-generated method stub
		mView.setTitle("还原");
		mView.setFileName(absolutePath);
	}

	void log(String text) {
		System.out.println("MainPresenter:" + text);
	}
}
